﻿@inject IStringLocalizer<MultiSelectBindingCollection> Localizer

<div>
    <div class="row g-3">
        <div class="col-12 col-sm-6">
            <MultiSelect Items="@Items" @bind-Value="@SelectedArrayValues" Max="4" Min="2" />
        </div>
        <div class="col-12 col-sm-6">
            <Button Icon="fa-solid fa-plus" Text="@Localizer["MultiSelectBindingCollectionAdd"]" OnClick="@AddListItems" class="me-1" />
            <Button Icon="fa-solid fa-minus" Text="@Localizer["MultiSelectBindingCollectionDecrease"]" OnClick="@RemoveListItems" />
            <Button Icon="fa-regular fa-trash-can" Text="@Localizer["MultiSelectBindingCollectionClean"]" OnClick="@ClearListItems" />
        </div>
    </div>
    <div class="mt-3">@(string.Join(",", SelectedArrayValues))</div>
</div>

@code {
    [NotNull]
    private List<SelectedItem>? Items { get; set; }

    [NotNull]
    private List<SelectedItem>? DataSource { get; set; }

    /// <summary>
    /// OnInitalized
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        // 初始化数据
        DataSource = new List<SelectedItem>
        {
            new SelectedItem ("Beijing", "北京"),
            new SelectedItem ("Shanghai", "上海"),
            new SelectedItem ("Guangzhou", "广州")
        };

        Items = GenerateDataSource(DataSource);
    }

    private static List<SelectedItem> GenerateDataSource(List<SelectedItem> source) => source.Select(i => new SelectedItem(i.Value, i.Text)).ToList();

    private IEnumerable<string> SelectedArrayValues { get; set; } = Enumerable.Empty<string>();

    private void AddListItems()
    {
        SelectedArrayValues = "Beijing,Shanghai".Split(',');
    }

    private void RemoveListItems()
    {
        SelectedArrayValues = new[] { "Beijing" };
    }

    private void ClearListItems()
    {
        SelectedArrayValues = Enumerable.Empty<string>();
    }
}
